package com.sast.user_backend.controller;


import cn.hutool.http.server.HttpServerRequest;
import com.sast.user_backend.common.ErrorCode;
import com.sast.user_backend.common.Response;
import com.sast.user_backend.exception.BusinessException;
import com.sast.user_backend.model.UserEditRequest;
import com.sast.user_backend.model.UserLoginRequest;
import com.sast.user_backend.model.UserRegisterRequest;
import com.sast.user_backend.model.UserVO;
import com.sast.user_backend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public Response<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long result = userService.register(userRegisterRequest);
        return Response.newSuccess(result);
    }

    @PostMapping("/login")
    public Response<UserVO> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        String userAccount = userLoginRequest.getAccount();
        String userPassword = userLoginRequest.getPassword();
        return Response.newSuccess(userService.login(userAccount,userPassword,request));
    }

    @PostMapping("/edit")
    public Response<Long> edit(@RequestBody UserEditRequest userEditRequest, HttpServletRequest request) {
        return Response.newSuccess(userService.edit(userEditRequest,request));
    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public Response<Boolean> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.logout(request);
        return Response.newSuccess(result);
    }

    @GetMapping("/hello")
    public String hello() {
        return "hello";
    }
}
